import Vue from 'vue'
import Router from 'vue-router'
import gas from './gas'
import account from './account'
import store from 'store/index'

Vue.use(Router)

const router = new Router({
  routes: [
    ...gas,
    ...account
  ]
})

router.beforeEach((to, from, next) => {
  // 判断该路由是否需要登录权限
  const token = store.state.user.token
  let is_activated = store.state.user.is_activated
  if (token && (to.path == '/login' || to.path == '/')) {
    next('/personal')
  } else {
    next();
  }

  if (to.meta.requireAuth) {
    if (!token) {
      next('/login')
    } else if (is_activated != 1) {
      next('/activated')
    } else {
      next();
    }
  } else {
    next();
  }

})

export default router
